# -*- coding: utf-8 -*-
'''
Created on Mar 9, 2013

@author: LONG HOANG GIANG
'''
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib2 import commonlib, html2text
import datetime
import re, json
from urlparse import urljoin
from Cheetah.Template import Template

templateDef = """\
#encoding UTF-8
<html>
    <head>
        <title>TOIYEUMOBILE</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta charset="UTF-8" />
        <link rel="stylesheet" type="text/css" href="../style.css" />
    </head>
    <body>
        <div id="main">
        <strong>GIỚI THIỆU</strong><hr />
            $content
            <p>&nbsp;</p>
            <p><b>Chúc các bạn có những phút giây vui vẻ và thoải mái !</b></p>
            <p style="text-align: left;"><b>Mọi thông tin xin liên hệ: <a href="mailto:toiyeumobile.org@gmail.com">toiyeumobile.org@gmail.com</b></a></p>
        </div>
    </body>
</html>
"""



class Manga24():
    
    _cookie_str = ''
    _url = ''
    _rootUrl = 'http://manga24h.com/'
    _output = ''
    
    def __init__(self, url):
        self._url = url
        self._output = "{0}-{1}".format(commonlib.extractText("/(\d+)/", url, 1), commonlib.extractText("/([^/]+)\.html", url, 1))
        self.load2Cookie()
    
    def load2Cookie(self):
        fp = commonlib.loadweb(self._rootUrl)
        self._cookie_str = fp.getcookie()
    
    def getDetail(self, item):
        tree = commonlib.loadweb(item['url']).build_tree()
        data = []
        for img in tree.xpath("//ul[@id='portfolio']//img"):
            src = img.get('src', '').strip()
            if src == '': continue
            print src
            data.append(src)
        return data
    
    def getChapters(self):
        tree = commonlib.loadweb(self._url, cookie=self._cookie_str).build_tree()
        data = []
        for node in tree.xpath("//table[@class='mytable']//th[@class='spec']/a"):
            title = commonlib.stringify(node)
            href = node.get('href', '')
            if href == '': continue
            href = urljoin(self._rootUrl, href)
            data.append({'title': title, 'url': href})
            print title, href

        introNode = tree.xpath("//div[@class='post']")[0]
        try:
            commonlib.Etree.clean(introNode.xpath(".//strong[contains(., 'Hãy bấm THANKS')]".decode('utf-8'))[0])
        except:
            pass
        commonlib.Etree.clean_following_sibling(introNode.xpath(".//div[@id='thanks']")[0], True)
        for label in ['Tình trạng', 'Ngày thêm', 'Nguồn', 'View']:
            commonlib.Etree.clean(introNode.xpath(".//strong[contains(., '{0}')]/..".format(label).decode('utf-8'))[0])
        intro = html2text.html2text(commonlib.Etree.tostring(introNode), textonly=True).encode('utf-8').strip()
        intro = re.sub(r"\n", "<br />", intro)
        html = Template(templateDef, searchList=[{'content': intro}])
        return data, str(html)
        
    def process(self):
        chapters, intro = self.getChapters()
        output = "/longhoanggiang/comic/{0}/".format(self._output)
        if not os.path.isdir(output): os.makedirs(output, 0777)
        commonlib.file_put_contents(output + 'intro.html', intro)
        data = []
        for chapter in chapters:
            chapterId = commonlib.crc32unsigned(chapter['url'])
            imgArr = self.getDetail(chapter)
            commonlib.gzip(output + chapterId, json.dumps(imgArr))
            data.append({'title': chapter['title'], 'chapterId': chapterId})
        commonlib.gzip(output + 'data', json.dumps(data))

if __name__ == '__main__':
    worker = Manga24('http://manga24h.com/29/O-Long-Vien.html')
    worker.process()
    print '> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)
    
    